home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / misc / fgets.c < prev    next >
C/C++ Source or Header  |  1996-03-20  |  4KB  |  191 lines

  1. #include <string.h>
  2. #include "misc.h"
  3. #include "../xconf/xconf.h"
  4.  
  5. /*
  6.     Read a line of a configuration file and process continuation line.
  7.     Return buf, or NULL si eof
  8.     Blank at the end of line are always stripped.
  9.     Everything following comcar is a comment. The line is cut before.
  10. */
  11. char *fgets_strip (
  12.     char *buf,
  13.     int sizebuf,
  14.     FILE *fin,
  15.     char contcar,        // Continuation char, generally backslash
  16.     char comcar,        // Comment character, generally #
  17.     int *noline,        // This will be updated and contain the line number
  18.                         // Can be NULL
  19.     int *empty)            // Will be != 0 if the line is empty and did not
  20.                         // even hold a comment.
  21. {
  22.     int nocomment = 1;        // No comments found ?
  23.     int contline=0;
  24.     char *debut = buf;
  25.     char *ret = NULL;
  26.     *buf = '\0';
  27.     *empty = 1;
  28.     while (fgets(buf,sizebuf,fin)!=NULL){
  29.         char *end = strip_end (buf);
  30.         char *pt = strchr(buf,comcar);
  31.         if (pt != NULL){
  32.             nocomment = 0;
  33.             *pt = '\0';
  34.             end = strip_end (buf);
  35.         }
  36.         if (noline != NULL) (*noline)++;
  37.         ret = debut;
  38.         if (contline){
  39.             char *pt = str_skip(buf);
  40.             if (pt > buf+1){
  41.                 strcpy (buf+1,pt);
  42.                 buf[0] = ' ';
  43.                 end-=(int)(pt-buf)-1;
  44.             }else if (pt == buf+1){
  45.                 buf[0] = ' ';
  46.             }
  47.         }
  48.         if (end > buf && *(end-1) == contcar){
  49.             if (end == buf+1 || *(end-2) != contcar){
  50.                 /* Continuation demandé */
  51.                 contline = 1;
  52.                 end--;
  53.                 *end = '\0';
  54.                 buf = end;
  55.             }else{
  56.                 *(end-1) = '\0';
  57.                 break;
  58.             }
  59.         }else{
  60.             break;
  61.         }
  62.     }
  63.     *empty = debut[0] == '\0' && nocomment;
  64.     return ret;
  65. }        
  66.  
  67. /*
  68.     Read a line of a configuration file and process continuation line.
  69.     Return buf, or NULL si eof
  70.     Blank at the end of line are always stripped.
  71.     Everything following comcar is a comment. The line is cut before.
  72. */
  73. char *fgets_strip (
  74.     char *buf,
  75.     int sizebuf,
  76.     FILE *fin,
  77.     char contcar,        // Continuation char, generally backslash
  78.     char comcar,        // Comment character, generally #
  79.     int *noline)        // This will be updated and contain the line number
  80.                         // Can be NULL
  81. {
  82.     int empty;
  83.     return fgets_strip (buf,sizebuf,fin,contcar,comcar,noline,&empty);
  84. }
  85.  
  86. /*
  87.     Read a line of a configuration file and process continuation line.
  88.     Return buf, or NULL si eof
  89.     Blank at the end of line are always stripped.
  90.     Everything following comcar is a comment. The line is cut before.
  91.  
  92.     Continuation character is \
  93.     Comment character is #
  94. */
  95. char *fgets_strip (
  96.     char *buf,
  97.     int sizebuf,
  98.     FILE *fin,
  99.     int *noline)        // This will be updated and contain the line number
  100.                         // Can be NULL
  101. {
  102.     int empty;
  103.     return fgets_strip (buf,sizebuf,fin,'\\','#',noline,&empty);
  104. }
  105.  
  106. /*
  107.     Read a line of text and process continuation lines
  108.     unlike fgets_strip, comment are return untouched.
  109.  
  110.     Return -1 if end of file, 0 if ok
  111. */
  112. int fgets_cont (char *buf, int size, FILE *fin)
  113. {
  114.     int ret = -1;
  115.     char tmp[500];
  116.     buf[0] = '\0';
  117.     while (fgets(tmp,size,fin) != NULL){
  118.         char sbuf[500];
  119.         str_strip(tmp,sbuf);
  120.         int len = strlen(sbuf);
  121.         strcpy (buf,sbuf);
  122.         buf += len;
  123.         size -= len;
  124.         ret = 0;
  125.         if (len == 0 || sbuf[len-1] != '\\') break;
  126.         *--buf = '\0';
  127.         size++;
  128.     }
  129.     return ret;
  130. }
  131.  
  132. /*
  133.     Read lines and accumulate comments.
  134.     buf will contain the first non comment, non empty line.
  135. */
  136. const char *fgets_comments (
  137.     char buf[],
  138.     int size,
  139.     FILE *fin,
  140.     SSTRING &comments)
  141. {
  142.     const char *ret = NULL;
  143.     while (fgets_cont(buf,size,fin)!=-1){
  144.         strip_end (buf);
  145.         char *pt = str_skip (buf);
  146.         if (pt[0] == '\0' || pt[0] == '#'){
  147.             /* #Specification: fgets_comment / strategy
  148.                 When parsing comment out of a configuration file, only
  149.                 the text is kept (the # is striped). This helps the
  150.                 edit process. Comments should be written back with
  151.                 comment_write().
  152.             */
  153.             if (pt[0] == '#') pt = str_skip(pt+1);
  154.             strcat (buf,"\n");
  155.             comments.append (pt);
  156.         }else{
  157.             ret = buf;
  158.             break;
  159.         }
  160.     }
  161.     return ret;
  162. }
  163.  
  164.  
  165. /*
  166.     Write back a bunch of comment lines
  167. */
  168. void comment_write (const SSTRING &str, FILE *fout)
  169. {
  170.     const char *pt = str.get();
  171.     while (*pt != '\0'){
  172.         const char *begin = pt;
  173.         pt = strchr(pt,'\n');
  174.         if (pt == begin){
  175.             fputc('\n',fout);
  176.             pt++;
  177.         }else if (pt != NULL){
  178.             char line[1000];
  179.             char *ptl = line;
  180.             while (begin < pt) *ptl++ = *begin++;
  181.             *ptl = '\0';
  182.             fprintf (fout,"# %s\n",line);
  183.             pt++;
  184.         }else{
  185.             fprintf (fout,"# %s\n",begin);
  186.                 break;
  187.         }
  188.     }
  189. }
  190.  
  191.